//
//  XInfoPage.m
//  food
//
//  Created by 郭桓 on 2019/4/16.
//  Copyright © 2019 XmaMini. All rights reserved.
//

#import "XInfoPage.h"
#import "ReadTool.h"
#import "XRCarouselView.h"
#import "QDCustomToastAnimator.h"
@interface XInfoPage ()<RTLabelDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UILabel *infolable;
@property(nonatomic) CGFloat viewWidth;
@property(nonatomic) CGFloat posY;
@property(nonatomic) bool hasBookMark;  //是否已经标记
- (CGFloat) getImageHeight:(UIImage*)img width:(CGFloat)width;

@end



@implementation XInfoPage

- (void)rtLabel:(id)rtLabel didSelectLinkWithURL:(NSURL*)url
{
    NSLog(@"did select url %@", url);
}


- (CGFloat) getImageHeight:(UIImage*)img width:(CGFloat)width
{
    CGSize size = img.size;
    return (width/size.width) * size.height;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if(self.hasBookMark){
        NSMutableSet *likeArray = [[[NSUserDefaults standardUserDefaults]objectForKey:@"bookmark"] mutableCopy];
        if(!likeArray){
            //不存在
            likeArray = [[NSMutableSet alloc] initWithObjects:self.title, nil];
        }else{
            if(![likeArray containsObject:self.title]){
                [likeArray addObject:self.title];
            }
        }
        
        if([likeArray count] > 0){
            [[NSUserDefaults standardUserDefaults]setObject:[likeArray allObjects] forKey:@"bookmark"];
            [[NSUserDefaults standardUserDefaults] synchronize];
        }
    }else{
        NSMutableSet *likeArray = [[[NSUserDefaults standardUserDefaults]objectForKey:@"bookmark"] mutableCopy];
        if(likeArray){
            [likeArray removeObject:self.title];
            [[NSUserDefaults standardUserDefaults] setObject:[likeArray allObjects] forKey:@"bookmark"];
            [[NSUserDefaults standardUserDefaults] synchronize];
        }
    }
}


- (void)viewDidLoad{
    [super viewDidLoad]; 
    self.viewWidth = self.view.bounds.size.width - QMUICMI.navBarAccessoryViewMarginLeft*3;
    self.posY = QMUICMI.tableViewGroupedSectionFooterContentInset.top;
    
    CGRect infoRect = CGRectMake(QMUICMI.navBarAccessoryViewMarginLeft, self.view.bounds.origin.y + QMUICMI.tableViewGroupedSectionFooterContentInset.top , self.viewWidth, self.view.bounds.size.height - QMUICMI.tableViewGroupedSectionFooterContentInset.top*2);
    
    self.scrollView = [[UIScrollView alloc]initWithFrame:infoRect];
    [self.view addSubview:self.scrollView];
    
    
    
//    NSMutableSet * set = [[NSMutableSet alloc] init];
//    3 [set addObject:@"one"];
//    4 [set addObject:@"two"];
//    5 [set addObject:@"two"]; //如果添加的元素有重复，实际只保留一个
    
 
//    if (!likeArray) {
//        likeArray = [[NSMutableSet alloc]initWithCapacity:0];
//    }
//    [likeArray addObject:data];
    
   
//    hasBookMark
    

    NSArray *info = [self.config objectForKey:@"info"];
    self.title = [self.config objectForKey:@"name"];
    
    
    
    
    
    
    self.hasBookMark = false;
    
    NSMutableSet *likeArray = [[[NSUserDefaults standardUserDefaults]objectForKey:@"bookmark"] mutableCopy];
    if(!likeArray){
        //不存在
    }else{
        if([likeArray containsObject:self.title]){
            self.hasBookMark = true;
        }
    }
    
    
    [self addRightBtn];
    
    for(int i = 0;i<info.count;i++){
        NSDictionary *item = [info objectAtIndex:i];
        NSArray *keysArray = [item allKeys];
        if(keysArray.count > 0){
            NSString *type = keysArray[0];
            if([type caseInsensitiveCompare:@"img"] == NSOrderedSame){
                UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(QMUICMI.navBarAccessoryViewMarginLeft,self.posY,self.viewWidth,0)];
                UIImage *image = UIImageMake([item valueForKey:@"img"]);
                img.image =image;
                CGFloat height = [self getImageHeight:image width:self.viewWidth];
                img.frame = CGRectMake(img.frame.origin.x, self.posY, img.frame.size.width, height);
                [self.scrollView addSubview:img];
                self.posY += height;
            }else if([type caseInsensitiveCompare:@"words"] == NSOrderedSame){
                RTLabel *lable =[[RTLabel alloc] initWithFrame:CGRectMake(QMUICMI.navBarAccessoryViewMarginLeft, self.posY + QMUICMI.tableViewGroupedSectionFooterContentInset.top , self.viewWidth, self.view.bounds.size.height - QMUICMI.tableViewGroupedSectionFooterContentInset.top*2)];
                [lable setFont:QMUICMI.navBarButtonFont];
                lable.delegate = self;
                lable.lineSpacing = 5;
                [lable setText:[item valueForKey:@"words"]];
                CGSize optimumSize = [lable optimumSize];
                self.richLable.frame = CGRectMake(lable.frame.origin.x, self.posY, lable.frame.size.width, optimumSize.height);
                self.posY += optimumSize.height;
                [self.scrollView addSubview:lable];
            }
        }
    }
    self.scrollView.contentSize = CGSizeMake(self.viewWidth,self.posY);
    
}


- (void)addRightBtn {
    
    if(self.hasBookMark){
        //已经存在了
        self.navigationItem.rightBarButtonItem = [UIBarButtonItem qmui_itemWithImage:UIImageMake(@"icon_bookmark_selected") target:self action:@selector(onClickedOKbtn)];
    }else{
        self.navigationItem.rightBarButtonItem = [UIBarButtonItem qmui_itemWithImage:UIImageMake(@"icon_bookmark") target:self action:@selector(onClickedOKbtn)];
    }
    AddAccessibilityLabel(self.navigationItem.rightBarButtonItem, @"收藏");

    
    
    
}

- (void)onClickedOKbtn {
    QMUITips *tips = [QMUITips createTipsToView:self.view];
    QDCustomToastAnimator *customAnimator = [[QDCustomToastAnimator alloc] initWithToastView:tips];
    tips.toastAnimator = customAnimator;
    if(self.hasBookMark){
        self.hasBookMark = false;
        [tips showInfo:@"已取消收藏" hideAfterDelay:2];
    }else{
        self.hasBookMark = true;
        [tips showInfo:@"已收藏"hideAfterDelay:2];
    }
    
    [self addRightBtn];
    
}

#pragma mark XRCarouselViewDelegate
- (void)carouselView:(XRCarouselView *)carouselView clickImageAtIndex:(NSInteger)index {
    NSLog(@"点击了第%ld张图片", index);
}


@end

